//
//  ZLCalendarSaleDetailCell.swift
//  TestDemo
//
//  Created by ayin on 2019/5/29.
//  Copyright © 2019 aYin. All rights reserved.
//  日历-发售详情-表cell

import UIKit

protocol ZLCalendarSaleDetailCellDelegate : NSObjectProtocol {
    //MARK: - 声明代理
    /// 点击头像事件代理
    ///
    /// - Parameter dataModel: 数据
    func avatorClickAction(dataModel: Any)
    
    /// 点击抽签/提醒按钮事件代理
    ///
    /// - Parameters:
    ///   - sender: 按钮本身
    ///   - dataModel: 数据
    func drawLotsAction(sender: UIButton, dataModel: Any)
    
    /// 点击交流箭头按钮事件代理
    ///
    /// - Parameters:
    ///   - sender: 按钮本身
    ///   - dataModel: 数据
    func exchangeAction(sender: UIButton, dataModel: Any)
}

extension ZLCalendarSaleDetailCellDelegate {
    func avatorClickAction(dataModel: Any) {}
    func drawLotsAction(sender: UIButton, dataModel: Any) {}
    func exchangeAction(sender: UIButton, dataModel: Any) {}
}

class ZLCalendarSaleDetailCell: UITableViewCell {
    //MARK: - 声明属性
    //内容view
    lazy var containView: UIView = {
        let containView = UIView()
        containView.backgroundColor = UIColor.white
        return containView
    }()
    
    //头像
    lazy var avatorImgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .scaleAspectFit
        imgView.backgroundColor = UIColor.red
        return imgView
    }()
    
    //标题
    lazy var titleLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangSemibold(size: 13.up), text: nil, textColor: UIColor.fc11, numberLine: 1, alignment: .left)
        return label
    }()
    
    //抽签/提醒我
    lazy var drawLotsBtn: UIButton = {
        let btn = UIButton.makeButton(font: UIFont.pingfangRegular(size: 11.up), title: "去抽签", titleColor: UIColor.white, image: nil, bgColor: UIColor.fc15)
        btn.cornerRadius(2)
        btn.addTarget(self, action: #selector(drawLotsBtnAction(sender:)), for: .touchUpInside)
        return btn
    }()
    
    //发售时间
    lazy var saleTimeLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 12.up), text: nil, textColor: UIColor.fc2, numberLine: 1, alignment: .left)
        return label
    }()
    
    //发售方式
    lazy var saleWayLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 12.up), text: nil, textColor: UIColor.fc2, numberLine: 1, alignment: .left)
        return label
    }()
    
    //安排
    lazy var arrangeLB: UILabel = {
        let label = UILabel.makeLabel(font: UIFont.pingfangRegular(size: 11.up), text: nil, textColor: UIColor.fc2, numberLine: 1, alignment: .left)
        return label
    }()
    
    //交流
    lazy var exchangeBtn: UIButton = {
        let btn = UIButton.makeButton(font: UIFont.pingfangRegular(size: 11.up), title: "43人正在陪跑交流", titleColor: UIColor.fc1, image: "calendar_exchange_arrow", bgColor: UIColor.clear)
//        btn.layoutButton(with: .imageRight, imageTitlespace: 3.0)
//        btn.setEnlargeEdgeWithTop(10, right: 10, bottom: 10, left: 10)
        btn.addTarget(self, action: #selector(exchangeBtnAction(sender:)), for: .touchUpInside)
        return btn
    }()
    
    lazy var cutLineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.bg4
        return view
    }()
    
    let hSpace: CGFloat = 15
    let vSpace: CGFloat = 18
    weak var delegate: ZLCalendarSaleDetailCellDelegate?
    
    //MARK: - 初始化
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 搭建UI
    func setupUI() {
        contentView.backgroundColor = UIColor.white
        contentView.addSubview(containView)
        containView.addSubview(avatorImgView)
        containView.addSubview(titleLB)
        containView.addSubview(drawLotsBtn)
        containView.addSubview(saleTimeLB)
        containView.addSubview(saleWayLB)
        containView.addSubview(arrangeLB)
        containView.addSubview(exchangeBtn)
        containView.addSubview(cutLineView)
        
        containView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        avatorImgView.cornerRadius(9.up)
        avatorImgView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(hSpace)
            make.top.equalToSuperview().offset(vSpace)
            make.width.height.equalTo(18.up)
        }
        
        titleLB.snp.makeConstraints { (make) in
            make.left.equalTo(avatorImgView.snp.right).offset(5)
            make.centerY.equalTo(avatorImgView)
            make.right.lessThanOrEqualToSuperview().offset(-hSpace - 62.up)
        }
        
        drawLotsBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-hSpace)
            make.top.equalToSuperview().offset(vSpace)
            make.width.equalTo(62.up)
            make.height.equalTo(32.up)
        }
        
        saleTimeLB.snp.makeConstraints { (make) in
            make.left.equalTo(avatorImgView)
            make.right.lessThanOrEqualTo(titleLB)
            make.top.equalTo(avatorImgView.snp.bottom).offset(10)
        }
        
        saleWayLB.snp.makeConstraints { (make) in
            make.left.equalTo(saleTimeLB)
            make.right.lessThanOrEqualToSuperview().offset(-hSpace)
            make.top.equalTo(saleTimeLB.snp.bottom).offset(5)
        }
        
        arrangeLB.snp.makeConstraints { (make) in
            make.left.equalTo(saleWayLB)
            make.top.equalTo(saleWayLB.snp.bottom).offset(5)
            make.bottom.equalToSuperview().offset(-vSpace)
        }
        
        exchangeBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-hSpace)
            make.centerY.equalTo(arrangeLB)
        }
        
        cutLineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(avatorImgViewClick(tap:)))
        avatorImgView.addGestureRecognizer(tap)
    }
    
    //MARK: - 刷新数据
    func refreshDatas (_ dataModel: Any = "") {
//        avatorImgView.eyee_webImage(with: "url", placeholder: nil)
        
        titleLB.text = "SNKRS-日区"
        
        saleTimeLB.attributedText = String.addAttributeStringForegroundColor(prefixString: "发售时间：", contentString: "05-16 11:00", prefixColor: UIColor.fc2, contentColor: UIColor.fc1)
        
        saleWayLB.attributedText = String.addAttributeStringForegroundColor(prefixString: "发售方式：", contentString: "线上抽签", prefixColor: UIColor.fc2, contentColor: UIColor.fc1)
        
        arrangeLB.text = "9844人想安排"
        
        exchangeBtn.setTitle("43人正在陪跑交流", for: .normal)
    }
    
    //MARK: - 事件
    //头像点击事件
    @objc func avatorImgViewClick(tap: UITapGestureRecognizer) {
        self.delegate?.avatorClickAction(dataModel: "")
    }
    
    //抽签/提醒我按钮点击事件
    @objc func drawLotsBtnAction(sender: UIButton) {
        self.delegate?.drawLotsAction(sender: sender, dataModel: "")
    }
    
    //交流按钮点击事件
    @objc func exchangeBtnAction(sender: UIButton) {
        self.delegate?.exchangeAction(sender: sender, dataModel: "")
    }
}
